<template>
	<div class="container">
		<el-tabs v-model="message">
			<el-tab-pane :label="`未读消息(${state.unread.length})`" name="first">
				<el-table :data="state.unread" :show-header="false" style="width: 100%">
					<el-table-column>
						<template #default="scope">
							<span class="message-title">{{ scope.row.title }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="place" width="180"></el-table-column>
					<el-table-column prop="date" width="180"></el-table-column>
					<el-table-column prop="content" width="180">
						<template #default="scope">
							<el-link :href="scope.row.content" :icon="Download" type="success">下载内容</el-link>
						</template>
					</el-table-column>
					<el-table-column prop="plan" width="180">
						<template #default="scope">
							<el-link :href="scope.row.plan" :icon="Download" type="warning">下载方案</el-link>
						</template>
					</el-table-column>
					<el-table-column width="120">
						<template #default="scope">
							<el-button size="small" @click="handleRead(scope.$index)">标为已读</el-button>
						</template>
					</el-table-column>
				</el-table>
				<div class="handle-row">
					<el-button type="primary">全部标为已读</el-button>
				</div>
			</el-tab-pane>
			<el-tab-pane :label="`已读消息(${state.read.length})`" name="second">
				<template v-if="message === 'second'">
					<el-table :data="state.read" :show-header="false" style="width: 100%">
						<el-table-column>
							<template #default="scope">
								<span class="message-title">{{ scope.row.title }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="place" width="180"></el-table-column>
						<el-table-column prop="date" width="180"></el-table-column>
						<el-table-column prop="content" width="180">
							<template #default="scope">
								<el-link :href="scope.row.content" :icon="Download" type="success">下载内容</el-link>
							</template>
						</el-table-column>
						<el-table-column prop="plan" width="180">
							<template #default="scope">
								<el-link :href="scope.row.plan" :icon="Download" type="warning">下载方案</el-link>
							</template>
						</el-table-column>
						<el-table-column width="120">
							<template #default="scope">
								<el-button type="danger" @click="handleDel(scope.$index)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<div class="handle-row">
						<el-button type="danger">删除全部</el-button>
					</div>
				</template>
			</el-tab-pane>
			<el-tab-pane :label="`回收站(${state.recycle.length})`" name="third">
				<template v-if="message === 'third'">
					<el-table :data="state.recycle" :show-header="false" style="width: 100%">
						<el-table-column>
							<template #default="scope">
								<span class="message-title">{{ scope.row.title }}</span>
							</template>
						</el-table-column>
						<el-table-column prop="place" width="180"></el-table-column>
						<el-table-column prop="date" width="180"></el-table-column>
						<el-table-column prop="content" width="180">
							<template #default="scope">
								<el-link :href="scope.row.content" :icon="Download" type="success">下载内容</el-link>
							</template>
						</el-table-column>
						<el-table-column prop="plan" width="180">
							<template #default="scope">
								<el-link :href="scope.row.plan" :icon="Download" type="warning">下载方案</el-link>
							</template>
						</el-table-column>
						<el-table-column width="120">
							<template #default="scope">
								<el-button @click="handleRestore(scope.$index)">还原</el-button>
							</template>
						</el-table-column>
					</el-table>
					<div class="handle-row">
						<el-button type="danger">清空回收站</el-button>
					</div>
				</template>
			</el-tab-pane>
		</el-tabs>
	</div>
</template>

<script setup lang="ts" name="tabs">
import { ref, reactive } from 'vue';
import axios from "../utils/request";
import { Download } from '@element-plus/icons-vue';


interface TableItem {
	[key: string]: string | number;
}

interface ConferenceData {
	preset_time: string;
	id: number;
}


const message = ref('first');
const state = reactive<{ unread: TableItem[], read: TableItem[], recycle: TableItem[] }>({
	unread: [
		{
			date: '2018-04-19 20:00:00',
			palce: '图书馆',
			title: '【数据库课设】成功验收刘恒谦说请喝奶茶'
		},
		{
			date: '2018-04-19 21:00:00',
			palce: '图书馆',
			title: '今晚12点整发大红包，先到先得'
		},
		// {
		// 	date: '2024-1-16 22:00:00',
		// 	title: '【会议通知】 第二次会议，议题'
		// }
	],
	read: [
		{
			date: '2018-04-19 20:00:00',
			palce: '图书馆',
			title: '【会议通知】第一次会议，议题'
		}
	],
	recycle: [
		{
			date: '2018-04-19 20:00:00',
			palce: '主楼',
			title: '【系统通知】该系统将于今晚凌晨2点到5点进行升级维护'
		}
	]
});


axios.get('/conference_situation/api/conference_id', {
	params: {
		person_id: localStorage.getItem('person_id'),
		notice: '已读'
	}
})
	.then((res: { data: { results: TableItem[] } }) => res.data.results)
	.then(situation => {

		axios
			.get("/conference/viewsets/")
			.then((res: { data: { results: TableItem[] } }) => res.data.results)
			.then(conference => conference.filter(i => situation.some(j => i.id === j.conference_id)))
			.then((conference) => conference.map((i) => {
				return {
					date: i.preset_time,
					place: i.place,
					title: i.title,
					plan:i.plan,
					content:i.content
				}
			}))
			.then(data => {
				state.read = data;
				console.log(state.read)
			})
	})

axios.get('/conference_situation/api/conference_id', {
	params: {
		person_id: localStorage.getItem('person_id'),
		notice: '未读'
	}
})
	.then((res: { data: { results: TableItem[] } }) => res.data.results)
	.then(situation => {
		axios
			.get("/conference/viewsets/")
			.then((res: { data: { results: TableItem[] } }) => res.data.results)
			.then(conference => conference.filter(i => situation.some(j => i.id === j.conference_id)))
			.then((conference) => conference.map((i) => {
				return {
					date: i.preset_time,
					place: i.place,
					title: i.title,
					plan:i.plan,
					content:i.content
				}
			}))
			.then(data => {
				state.unread = data;
				console.log(state.unread)
			})
	})

axios.get('/conference_situation/api/conference_id', {
	params: {
		person_id: localStorage.getItem('person_id'),
		notice: '删除'
	}
})
	.then((res: { data: { results: TableItem[] } }) => res.data.results)
	.then(situation => {
		axios
			.get("/conference/viewsets/")
			.then((res: { data: { results: TableItem[] } }) => res.data.results)
			.then(conference => conference.filter(i => situation.some(j => i.id === j.conference_id)))
			.then((conference) => conference.map((i) => {
				return {
					date: i.preset_time,
					place: i.place,
					title: i.title,
					plan:i.plan,
					content:i.content
				}
			}))
			.then(data => {
				state.recycle = data;
				console.log(state.recycle)
			})
	})


const handleRead = (index: number) => {
	const item = state.unread.splice(index, 1);
	state.read = item.concat(state.read);
};
const handleDel = (index: number) => {
	const item = state.read.splice(index, 1);
	state.recycle = item.concat(state.recycle);
};
const handleRestore = (index: number) => {
	const item = state.recycle.splice(index, 1);
	state.read = item.concat(state.read);
};

</script>

<style>
.message-title {
	cursor: pointer;
}

.handle-row {
	margin-top: 30px;
}
</style>
